Java Interface

In Java, an interface is a means for achieving abstraction. The Java interface can only have abstract methods, not method bodies. In Java, it is used to achieve abstraction and multiple inheritance.

The interface is the blueprint of a class which contains only abstract method(s).

Interface cannot be instantiated like abstract class, but it can contain instance of the class that implemented it.

Syntax for interface

interface <interface_name>{  
      
    // constant fields  
    // abstact methods
}  

The interface can only contain abstract methods and constant fields.

An interface has to be implemented by the class and an interface has to extends an other interface.

enter image description here

Example program for inheritance

// interface creation
interface Language {
  public void setLanguage(String name);
  public String getLanguage();
}

// class implements interface
class JavaProgramming implements Language {
  String language= "";
  public void setLanguage(String name){
      language = name;
  }
  
  public String getLanguage(){
      return language;
  }
}

class Main {
  public static void main(String[] args) {
    JavaProgramming language = new JavaProgramming();
    language.setLanguage("Java");
    String languageName =  language.getLanguage();
    System.out.println("Language is "+languageName);
  }
}

Output

Language is Java

In the above program, we have an interface Language with 2 abstract methods setLanguage() and getLanguage().

Next, we have a class JavaProgramming which implements the Language interface. So JavaProgramming class will have to implement setLanguage() and getLanguage() methods.

Later, we create object for the JavaProgramming class and set the name of the language using setLanguage() method and get the name of the language using getLanguage().

Multiple inheritance with interface

With the help of interface we can implement the multiple inheritance since multiple inheritance is not supported in java.

enter image description here

Example for multiple inheritance with interface

interface Printable {
    void printPage();
}
interface Scannable {
    void scanPage();
}
class Printer implements Printable, Scannable {
    public void printPage() {
        System.out.println("Printable");
    }
    public void scanPage() {
        System.out.println("Scannable");
    }
}

class Main {

    public static void main(String args[]) {
        Printer obj = new Printer();
        obj.printPage();
        obj.scanPage();
    }
}

Output

Printable
Scannable

In the above program we have created 2 interfaces Printable and Scannable with their own abstract methods.

Next, these two interfaces are implemented by Printer class. This Printer class then implements both the printPage and scanPage methods.

Now in the Main class we create an object for the Printer class and call the printPage and scanPage methods.

Interface inheritance

It is possible to extend an interface A by an other interface B. In such case, the class that implements the interface B, has to implement the abstract methods of interface A and B.

Example program for interface inheritance

interface IScannable {
  void scanPage();
}
interface IPrinter extends IScannable {
  void printPage();
}
class Printer implements IPrinter {
  public void printPage() {
    System.out.println("Printable");
  }
  public void scanPage() {
    System.out.println("Scannable");
  }
}

class Main {

  public static void main(String args[]) {
    IPrinter obj = new Printer();
    obj.printPage();
    obj.scanPage();
  }
}

In the above program, we created an interface IScannable and this interface is extended by IPrinter interface.

Next, in the Printer class we implement the IPrinter interface. So it implements the abstract methods of IPrinter as well as IScannable.

Next, in the Main class we create an object for the Printer class and assign it to IPrinter object.

Now, using the IPrinter object we call the printPage() and scanPage() methods.

Points to remember

  • Interfaces contains only the abstract methods.
  • We cannot instantiate the interface.
  • We can create an instance of a class that implements an interface and assign it to the object of that interface.
  • Interface cannot contain function with definition.

Most Read